﻿@model KL.Store.Web.Models.ProductInfoModel
@using KL.Store.Core
@{
    ViewBag.Title = "";
    Layout = null;
}
<table class="table table-bordered" id="EditProductInfo">
    @foreach (var info in Model.ProductInfos)
    {
        <tr>
            <td style="width: 50%;">
                <input type="text" data-type="Type" data-satirno="@info.SatirNo" data-original="@info.Type" value="@info.Type" class="form-control" />
            </td>
            <td style="width: 50%;">
                <input type="text" data-type="Information" data-satirno="@info.SatirNo" data-original="@info.Information" value="@info.Information" class="form-control" />
            </td>
            <td>
                <a href="javascript:void(0)" class="RemoveItem" data-satirno="@info.SatirNo"><i class="fa fa-remove fa-2x"></i></a>
            </td>
        </tr>
    }
</table>
<div class="form-group">
    <div class="col-xs-8 first">
        <form class="form-inline" role="form">
            <div class="form-group">
                <input type="text" name="addType" id="addType" class="form-control" />
            </div>

            <div class="form-group">
                <input type="text" name="addInformation" id="addInformation" class="form-control" />
            </div>

            <div class="form-group">
                <button type="button" id="addSave" class="btn btn-custom1">Ekle</button>
            </div>
        </form>
    </div>
    <div class="col-xs-4 first alignright">
        <button type="button" class="btn btn-custom1" id="closeForm">Kapat</button>
        <button type="button" id="kaydetHepsini" class="btn btn-custom1">Kaydet</button>
    </div>
</div>
<div id="piResult" style="display: none;"></div>
<div class="loading"></div>
<script>

    $('button#kaydetHepsini').click(function ()
    {
        var refreshFields = false;
        $('#additionProperties .loading').show();
        var alanlar = $('table#EditProductInfo tr td input[type=text]');
        var alanSayisi = alanlar.length;
        //alert(alanSayisi);
        var alanSayaci = 0;
        alanlar.each(function ()
        {

            var DataType = $(this).data('type');
            var DataSatirNo = $(this).data('satirno');
            var DataOriginal = $(this).data('original');
            var DataValue = $(this).val();
            var DataProduct = '@(Model.ProductId)';

            if (DataOriginal != DataValue)
            {
                //alert(DataType + " " + DataOriginal + " " + DataValue + " " + DataProduct);
                $('#piResult').load('/Product/ProductInfoSave', { datatype: DataType, datasatirno: DataSatirNo, datavalue: DataValue, dataproduct: DataProduct }, function ()
                {

                });
            }
            alanSayaci++;
            //alert(DataValue + " " + alanSayaci)

            if (alanSayaci == alanSayisi)
            {
                $('#additionProperties').load('/Product/EditProductInfo', { productId: '@(Model.ProductId)' }, function ()
                {
                    $('#additionProperties .loading').hide();
                    alert('Kaydedildi.');
                });

            }

        });
    });

    $('.RemoveItem').click(function ()
    {
        if (confirm('Silmek istediğinizden emin misiniz?'))
        {
            var DataSatirNo = $(this).data('satirno');
            var DataProduct = '@(Model.ProductId)';
            //alert(DataType + " " + DataInformation + " " + DataProduct);
            $('#piResult').load('/Product/ProductInfoRemove', { ProductId: DataProduct, SatirNo: DataSatirNo }, function ()
            {
                $('#additionProperties').load('/Product/EditProductInfo', { productId: '@(Model.ProductId)' });
            });
        }
    });

    //ekle
    $('button#addSave').click(function ()
    {
        var DataType = $('input#addType').val();
        var DataInformation = $('input#addInformation').val();
        var DataProduct = '@(Model.ProductId)';
       $('#piResult').load('/Product/ProductInfoInsert', { ProductId: DataProduct, Type: DataType, Information: DataInformation }, function ()
       {
           $('input#addType').val('');
           $('input#addInformation').val('');
           $('#additionProperties').load('/Product/EditProductInfo', { productId: '@(Model.ProductId)' });
        });
   });

</script>
